import path from 'path';
import fs from 'fs';
import { spawn } from 'child_process';

export function replaceFileName(filePath: string, toPublicName: string, ext: string = 'jpg'){
    const basePath = filePath;
    const reg = /[0-9]+/g;
    console.log(basePath);
    const dirArr = fs.readdirSync(basePath);
    dirArr.sort((a, b) => Number((a.match(reg))?.join('')) > Number(b.match(reg)?.join('')) ? 1 : -1 );

    let isSort = true;
    // 校验是否是升序的，如果不是，则退出
    dirArr.reduce((a, b) => {
        if( isSort ) {
            isSort = Number(a.match(reg)?.join('')) < Number(b.match(reg)?.join(''));
        };
        return b;
    });
    if( !isSort ) {
        console.log('排序失败，无法进行下一步');
        console.log(dirArr);
        return;
    }
    const bufferDataList:{
        fileName: string;
        data: Buffer;
        originName: string;
    }[] = [];
    dirArr.forEach((item, index) => {
        try {
            const replaceBeforeData = fs.readFileSync(path.join(basePath, item));
            bufferDataList.push({
                fileName: path.join(basePath, `${toPublicName}${index + 1}.${ext}`),
                originName: path.join(basePath, item),
                data: replaceBeforeData
            });
            
        } catch (error) {
            return;
        }
    });
    if( bufferDataList.length !== dirArr.length ) {
        console.log('读取文件失败')
        return false;
    }
    const fileExtArr = dirArr[0].split('.');
    let fileExt = null;
    if( fileExtArr.length >= 2 && dirArr[0].lastIndexOf('.') !== -1) {
        fileExt = fileExtArr.pop();
    }else {
        return false;
    }
    const exec = spawn('rm',['-rf', '*.' + fileExt], {
        shell: true,
        cwd: path.join(basePath)
    });
    exec.stdout.on('data', (data) => {
        console.log(data.toString());
    });
    exec.on('error', (data) => {
        console.log('error', data.toString());
    });
    exec.on('close', () => {
        bufferDataList.forEach(item => {
            fs.writeFileSync(item.fileName, item.data, {
                flag: 'a+'
            });
            console.log(`${item.originName}   ====>   ${item.fileName}`);
        })
        console.log('修改完成');
    });
}




// 迁移数据所用函数
export function oldToNew() {
    var a = [
        {"id":1,"etage":"RDC ","nom":"LOT-A001","prix":80000,"surface":20,"piece":"T1","nomcomplet":"RDC / LOT-A001 / 80 000€ / 20.43m² / T1","solid":"\\house\\3D\\LOT_A001\\index.html","pic":"\\house\\A\\LOT_A001\\index.html","surfaces":"\\house\\2D\\PL-A001.jpg","yellows":"..\\house\\ren\\PLA001.html"},
        {"id":2,"etage":"RDC ","nom":"LOT-A002","prix":120000,"surface":35,"piece":"T1","nomcomplet":"RDC / LOT-A002 / 120 000€ / 34.60m² / T1","solid":"\\house\\3D\\LOT_A002\\index.html","pic":"\\house\\A\\LOT_A002\\index.html","surfaces":"\\house\\2D\\PL-A002.jpg","yellows":"..\\house\\ren\\PLA002.html"},
        {"id":3,"etage":"RDC ","nom":"LOT-A003","prix":72000,"surface":18,"piece":"T1","nomcomplet":"RDC / LOT-A003 / 72 000€ / 18.31m² / T1","solid":"\\house\\3D\\LOT_A003\\index.html","pic":"\\house\\A\\LOT_A003\\index.html","surfaces":"\\house\\2D\\PL-A003.jpg","yellows":"..\\house\\ren\\PLA003.html"},
        {"id":4,"etage":"RDC ","nom":"LOT-A004","prix":160000,"surface":41,"piece":"T2","nomcomplet":"RDC / LOT-A004 / 160 000€ / 41.39m² / T2","solid":"\\house\\3D\\LOT_A004\\index.html","pic":"\\house\\A\\LOT_A004\\index.html","surfaces":"\\house\\2D\\PL-A004.jpg","yellows":"..\\house\\ren\\PLA004.html"},
        {"id":5,"etage":"RDC ","nom":"LOT-A005","prix":240000,"surface":59,"piece":"T3","nomcomplet":"RDC / LOT-A005 / 240 000€ / 58.99m² / T3","solid":"\\house\\3D\\LOT_A005\\index.html","pic":"\\house\\A\\LOT_A005\\index.html","surfaces":"\\house\\2D\\PL-A005.jpg","yellows":"..\\house\\ren\\PLA005.html"},
        {"id":6,"etage":"RDC ","nom":"LOT-B001","prix":230000,"surface":56,"piece":"T3","nomcomplet":"RDC / LOT-B001 / 230 000€ / 56.23m² / T3","solid":"\\house\\3D\\LOT_B001\\index.html","pic":"\\house\\A\\LOT_B001\\index.html","surfaces":"\\house\\2D\\PL-B001.jpg","yellows":"..\\house\\ren\\PLB001.html"},
        {"id":7,"etage":"RDC ","nom":"LOT-B002","prix":150000,"surface":38,"piece":"T2","nomcomplet":"RDC / LOT-B002 / 150 000€ / 37.79m² / T2","solid":"\\house\\3D\\LOT_B002\\index.html","pic":"\\house\\A\\LOT_B002\\index.html","surfaces":"\\house\\2D\\PL-B002.jpg","yellows":"..\\house\\ren\\PLB002.html"},
        {"id":8,"etage":"RDC ","nom":"LOT-B003","prix":250000,"surface":59,"piece":"T3","nomcomplet":"RDC / LOT-B003 / 250 000€ / 59.23m² / T3","solid":"\\house\\3D\\LOT_B003\\index.html","pic":"\\house\\A\\LOT_B003\\index.html","surfaces":"\\house\\2D\\PL-B003.jpg","yellows":"..\\house\\ren\\PLB003.html"},
        {"id":9,"etage":"RDC ","nom":"LOT-B004","prix":152000,"surface":39,"piece":"T2","nomcomplet":"RDC / LOT-B004 / 152 000€ / 38.68m² / T2","solid":"\\house\\3D\\LOT_B004\\index.html","pic":"\\house\\A\\LOT_B004\\index.html","surfaces":"\\house\\2D\\PL-B004.jpg","yellows":"..\\house\\ren\\PLB004.html"},
        {"id":10,"etage":"R1","nom":"LOT-A101","prix":241200,"surface":64,"piece":"T3","nomcomplet":"R1 / LOT-A101 / 241 200€ / 63.54m² / T3","solid":"\\house\\3D\\LOT_A101\\index.html","pic":"\\house\\A\\LOT_A101\\index.html","surfaces":"\\house\\2D\\PL-A101.jpg","yellows":"..\\house\\ren\\PLA101.html"},
        {"id":11,"etage":"R1","nom":"LOT-A102","prix":240000,"surface":60,"piece":"T3","nomcomplet":"R1 / LOT-A102 / 240 000€ / 60.05m² / T3","solid":"\\house\\3D\\LOT_A102\\index.html","pic":"\\house\\A\\LOT_A102\\index.html","surfaces":"\\house\\2D\\PL-A102.jpg","yellows":"..\\house\\ren\\PLA102.html"},
        {"id":12,"etage":"R1","nom":"LOT-A103","prix":236000,"surface":59,"piece":"T3","nomcomplet":"R1 / LOT-A103 / 236 000€ / 58.96m² / T3","solid":"\\house\\3D\\LOT_A103\\index.html","pic":"\\house\\A\\LOT_A103\\index.html","surfaces":"\\house\\2D\\PL-A103.jpg","yellows":"..\\house\\ren\\PLA103.html"},
        {"id":13,"etage":"R1","nom":"LOT-A104","prix":237000,"surface":59,"piece":"T3","nomcomplet":"R1 / LOT-A104 / 237 000€ / 58.99m² / T3","solid":"\\house\\3D\\LOT_A104\\index.html","pic":"\\house\\A\\LOT_A104\\index.html","surfaces":"\\house\\2D\\PL-A104.jpg","yellows":"..\\house\\ren\\PLA104.html"},
        {"id":14,"etage":"R1","nom":"LOT-A105","prix":170000,"surface":41,"piece":"T2","nomcomplet":"R1 / LOT-A105 / 170 000€ / 40.63m² / T2","solid":"\\house\\3D\\LOT_A105\\index.html","pic":"\\house\\A\\LOT_A105\\index.html","surfaces":"\\house\\2D\\PL-A105.jpg","yellows":"..\\house\\ren\\PLA105.html"},
        {"id":15,"etage":"R1","nom":"LOT-B101","prix":200000,"surface":50,"piece":"T2","nomcomplet":"R1 / LOT-B101 / 200 000€ / 50.21m² / T2","solid":"\\house\\3D\\LOT_B101\\index.html","pic":"\\house\\A\\LOT_B101\\index.html","surfaces":"\\house\\2D\\PL-A106.jpg","yellows":"..\\house\\ren\\PLA106.html"},
        {"id":16,"etage":"R1","nom":"LOT-B102","prix":199000,"surface":50,"piece":"T2","nomcomplet":"R1 / LOT-B102 / 199 000€ / 50.18m² / T2","solid":"\\house\\3D\\LOT_B102\\index.html","pic":"\\house\\A\\LOT_B102\\index.html","surfaces":"\\house\\2D\\PL-B101.jpg","yellows":"..\\house\\ren\\PLB101.html"},
        {"id":17,"etage":"R1","nom":"LOT-B103","prix":220000,"surface":55,"piece":"T3","nomcomplet":"R1 / LOT-B103 / 220 000€ / 55.35m² / T3","solid":"\\house\\3D\\LOT_B103\\index.html","pic":"\\house\\A\\LOT_B103\\index.html","surfaces":"\\house\\2D\\PL-B102.jpg","yellows":"..\\house\\ren\\PLB102.html"},
        {"id":18,"etage":"R1","nom":"LOT-B104","prix":240400,"surface":61,"piece":"T3","nomcomplet":"R1 / LOT-B104 / 240 400€ / 61.03m² / T3","solid":"\\house\\3D\\LOT_B104\\index.html","pic":"\\house\\A\\LOT_B104\\index.html","surfaces":"\\house\\2D\\PL-B103.jpg","yellows":"..\\house\\ren\\PLB103.html"},
        {"id":19,"etage":"R2","nom":"LOT-A201","prix":241200,"surface":64,"piece":"T3","nomcomplet":"R2 / LOT-A201 / 241 200€ / 63.54m² / T3","solid":"\\house\\3D\\LOT_A201\\index.html","pic":"\\house\\A\\LOT_A201\\index.html","surfaces":"\\house\\2D\\PL-A201.jpg","yellows":"..\\house\\ren\\PLA201.html"},
        {"id":20,"etage":"R2","nom":"LOT-A202","prix":240000,"surface":60,"piece":"T3","nomcomplet":"R2 / LOT-A202 / 240 000€ / 60.05m² / T3","solid":"\\house\\3D\\LOT_A202\\index.html","pic":"\\house\\A\\LOT_A202\\index.html","surfaces":"\\house\\2D\\PL-A202.jpg","yellows":"..\\house\\ren\\PLA202.html"},
        {"id":21,"etage":"R2","nom":"LOT-A203","prix":236000,"surface":59,"piece":"T3","nomcomplet":"R2 / LOT-A203 / 236 000€ / 58.96m² / T3","solid":"\\house\\3D\\LOT_A203\\index.html","pic":"\\house\\A\\LOT_A203\\index.html","surfaces":"\\house\\2D\\PL-A203.jpg","yellows":"..\\house\\ren\\PLA203.html"},
        {"id":22,"etage":"R2","nom":"LOT-A204","prix":237000,"surface":59,"piece":"T3","nomcomplet":"R2 / LOT-A204 / 237 000€ / 58.99m² / T3","solid":"\\house\\3D\\LOT_A204\\index.html","pic":"\\house\\A\\LOT_A204\\index.html","surfaces":"\\house\\2D\\PL-A204.jpg","yellows":"..\\house\\ren\\PLA204.html"},
        {"id":23,"etage":"R2","nom":"LOT-A205","prix":170000,"surface":41,"piece":"T2","nomcomplet":"R2 / LOT-A205 / 170 000€ / 40.63m² / T2","solid":"\\house\\3D\\LOT_A205\\index.html","pic":"\\house\\A\\LOT_A205\\index.html","surfaces":"\\house\\2D\\PL-A205.jpg","yellows":"..\\house\\ren\\PLA205.html"},
        {"id":24,"etage":"R2","nom":"LOT-B201","prix":159000,"surface":39,"piece":"T2","nomcomplet":"R2 / LOT-B201 / 159 000€ / 39.21m² / T2","solid":"\\house\\3D\\LOT_B201\\index.html","pic":"\\house\\A\\LOT_B201\\index.html","surfaces":"\\house\\2D\\PL-B201.jpg","yellows":"..\\house\\ren\\PLB201.html"},
        {"id":25,"etage":"R2","nom":"LOT-B202","prix":159800,"surface":39,"piece":"T2","nomcomplet":"R2 / LOT-B202 / 159 800€ / 39.46m² / T2","solid":"\\house\\3D\\LOT_B202\\index.html","pic":"\\house\\A\\LOT_B202\\index.html","surfaces":"\\house\\2D\\PL-B202.jpg","yellows":"..\\house\\ren\\PLB202.html"},
        {"id":26,"etage":"R2","nom":"LOT-B203","prix":152000,"surface":38,"piece":"T2","nomcomplet":"R2 / LOT-B203 / 152 000€ / 37.79m² / T2","solid":"\\house\\3D\\LOT_B203\\index.html","pic":"\\house\\A\\LOT_B203\\index.html","surfaces":"\\house\\2D\\PL-B203.jpg","yellows":"..\\house\\ren\\PLB203.html"},
        {"id":27,"etage":"R2","nom":"LOT-B204","prix":239400,"surface":59,"piece":"T3","nomcomplet":"R2 / LOT-B204 / 239 400€ / 59.23m² / T3","solid":"\\house\\3D\\LOT_B204\\index.html","pic":"\\house\\A\\LOT_B204\\index.html","surfaces":"\\house\\2D\\PL-B204.jpg","yellows":"..\\house\\ren\\PLB204.html"},
        {"id":28,"etage":"R2","nom":"LOT-B205","prix":158000,"surface":39,"piece":"T2","nomcomplet":"R2 / LOT-B205 / 158 000€ / 38.68m² / T2","solid":"\\house\\3D\\LOT_B205\\index.html","pic":"\\house\\A\\LOT_B205\\index.html","surfaces":"\\house\\2D\\PL-B205.jpg","yellows":"..\\house\\ren\\PLB205.html"},
        {"id":29,"etage":"R3","nom":"LOT-A301","prix":241200,"surface":64,"piece":"T3","nomcomplet":"R3 / LOT-A101 / 241 200€ / 63.54m² / T3","solid":"\\house\\3D\\LOT_A301\\index.html","pic":"\\house\\A\\LOT_A301\\index.html","surfaces":"\\house\\2D\\PL-A301.jpg","yellows":"..\\house\\ren\\PLA301.html"},
        {"id":30,"etage":"R3","nom":"LOT-A302","prix":240000,"surface":60,"piece":"T3","nomcomplet":"R3 / LOT-A102 / 240 000€ / 60.05m² / T3","solid":"\\house\\3D\\LOT_A302\\index.html","pic":"\\house\\A\\LOT_A302\\index.html","surfaces":"\\house\\2D\\PL-A302.jpg","yellows":"..\\house\\ren\\PLA302.html"},
        {"id":31,"etage":"R3","nom":"LOT-A303","prix":236000,"surface":59,"piece":"T3","nomcomplet":"R3 / LOT-A103 / 236 000€ / 58.96m² / T3","solid":"\\house\\3D\\LOT_A303\\index.html","pic":"\\house\\A\\LOT_A303\\index.html","surfaces":"\\house\\2D\\PL-A303.jpg","yellows":"..\\house\\ren\\PLA303.html"},
        {"id":32,"etage":"R3","nom":"LOT-A304","prix":237000,"surface":59,"piece":"T3","nomcomplet":"R3 / LOT-A104 / 237 000€ / 58.99m² / T3","solid":"\\house\\3D\\LOT_A304\\index.html","pic":"\\house\\A\\LOT_A304\\index.html","surfaces":"\\house\\2D\\PL-A304.jpg","yellows":"..\\house\\ren\\PLA304.html"},
        {"id":33,"etage":"R3","nom":"LOT-A305","prix":170000,"surface":41,"piece":"T2","nomcomplet":"R3 / LOT-A105 / 170 000€ / 40.63m² / T2","solid":"\\house\\3D\\LOT_A305\\index.html","pic":"\\house\\A\\LOT_A305\\index.html","surfaces":"\\house\\2D\\PL-A305.jpg","yellows":"..\\house\\ren\\PLA305.html"},
        {"id":34,"etage":"R3","nom":"LOT-B301","prix":159000,"surface":39,"piece":"T2","nomcomplet":"R3 / LOT-B301 / 159 000€ / 39.21m² / T2","solid":"\\house\\3D\\LOT_B301\\index.html","pic":"\\house\\A\\LOT_B301\\index.html","surfaces":"\\house\\2D\\PL-B301.jpg","yellows":"..\\house\\ren\\PLB301.html"},
        {"id":35,"etage":"R3","nom":"LOT-B302","prix":159800,"surface":39,"piece":"T2","nomcomplet":"R3 / LOT-B302 / 159 800€ / 39.46m² / T2","solid":"\\house\\3D\\LOT_B302\\index.html","pic":"\\house\\A\\LOT_B302\\index.html","surfaces":"\\house\\2D\\PL-B302.jpg","yellows":"..\\house\\ren\\PLB302.html"},
        {"id":36,"etage":"R3","nom":"LOT-B303","prix":152000,"surface":38,"piece":"T2","nomcomplet":"R3 / LOT-B303 / 152 000€ / 37.79m² / T2","solid":"\\house\\3D\\LOT_B303\\index.html","pic":"\\house\\A\\LOT_B303\\index.html","surfaces":"\\house\\2D\\PL-B303.jpg","yellows":"..\\house\\ren\\PLB303.html"},
        {"id":37,"etage":"R3","nom":"LOT-B304","prix":239400,"surface":59,"piece":"T3","nomcomplet":"R3 / LOT-B304 / 239 400€ / 59.23m² / T3","solid":"\\house\\3D\\LOT_B304\\index.html","pic":"\\house\\A\\LOT_B304\\index.html","surfaces":"\\house\\2D\\PL-B304.jpg","yellows":"..\\house\\ren\\PLB304.html"},
        {"id":38,"etage":"R3","nom":"LOT-B305","prix":158000,"surface":39,"piece":"T2","nomcomplet":"R3 / LOT-B305 / 158 000€ / 38.68m² / T2","solid":"\\house\\3D\\LOT_B305\\index.html","pic":"\\house\\A\\LOT_B305\\index.html","surfaces":"\\house\\2D\\PL-B305.jpg","yellows":"..\\house\\ren\\PLB305.html"},
        {"id":39,"etage":"R4","nom":"LOT-A401","prix":180000,"surface":45,"piece":"T2","nomcomplet":"R4 / LOT-A401 / 180 000€ / 45.10m² / T2","solid":"\\house\\3D\\LOT_A401\\index.html","pic":"\\house\\A\\LOT_A401\\index.html","surfaces":"\\house\\2D\\PL-A401.jpg","yellows":"..\\house\\ren\\PLA401.html"},
        {"id":40,"etage":"R4","nom":"LOT-A402","prix":220000,"surface":56,"piece":"T3","nomcomplet":"R4 / LOT-A402 / 220 000€ / 55.57m² / T3","solid":"\\house\\3D\\LOT_A402\\index.html","pic":"\\house\\A\\LOT_A402\\index.html","surfaces":"\\house\\2D\\PL-A402.jpg","yellows":"..\\house\\ren\\PLA402.html"},
        {"id":41,"etage":"R4","nom":"LOT-A403","prix":212000,"surface":53,"piece":"T2","nomcomplet":"R4 / LOT-A403 /212 000€ / 53.22m² / T2","solid":"\\house\\3D\\LOT_A403\\index.html","pic":"\\house\\A\\LOT_A403\\index.html","surfaces":"\\house\\2D\\PL-A403.jpg","yellows":"..\\house\\ren\\PLA403.html"},
        {"id":42,"etage":"R4","nom":"LOT-A404","prix":234000,"surface":59,"piece":"T3","nomcomplet":"R4 / LOT-A404 /234 000€ / 58.99m² / T3","solid":"\\house\\3D\\LOT_A404\\index.html","pic":"\\house\\A\\LOT_A404\\index.html","surfaces":"\\house\\2D\\PL-A404.jpg","yellows":"..\\house\\ren\\PLA404.html"},
        {"id":43,"etage":"R4","nom":"LOT-A405","prix":164000,"surface":41,"piece":"T2","nomcomplet":"R4 / LOT-A405 /164 000€ / 40.63m² / T2","solid":"\\house\\3D\\LOT_A405\\index.html","pic":"\\house\\A\\LOT_A405\\index.html","surfaces":"\\house\\2D\\PL-A405.jpg","yellows":"..\\house\\ren\\PLA405.html"},
        {"id":44,"etage":"R4","nom":"LOT-B401","prix":120000,"surface":30,"piece":"T1","nomcomplet":"R4 / LOT-B401 /120 000€ / 29.81m² / T1","solid":"\\house\\3D\\LOT_B401\\index.html","pic":"\\house\\A\\LOT_B401\\index.html","surfaces":"\\house\\2D\\PL-B401.jpg","yellows":"..\\house\\ren\\PLB401.html"},
        {"id":45,"etage":"R4","nom":"LOT-B402","prix":164000,"surface":41,"piece":"T2","nomcomplet":"R4 / LOT-B402 /164 000€ / 41.33m² / T2","solid":"\\house\\3D\\LOT_B402\\index.html","pic":"\\house\\A\\LOT_B402\\index.html","surfaces":"\\house\\2D\\PL-B402.jpg","yellows":"..\\house\\ren\\PLB402.html"},
        {"id":46,"etage":"R4","nom":"LOT-B403","prix":150000,"surface":38,"piece":"T2","nomcomplet":"R4 / LOT-B403 /150 000€ / 37.79m² / T2","solid":"\\house\\3D\\LOT_B403\\index.html","pic":"\\house\\A\\LOT_B403\\index.html","surfaces":"\\house\\2D\\PL-B403.jpg","yellows":"..\\house\\ren\\PLB403.html"},
        {"id":47,"etage":"R4","nom":"LOT-B404","prix":282400,"surface":76,"piece":"T4","nomcomplet":"R4 / LOT-B404 /282 400€ / 76.15m² / T4","solid":"\\house\\3D\\LOT_B404\\index.html","pic":"\\house\\A\\LOT_B404\\index.html","surfaces":"\\house\\2D\\PL-B404.jpg","yellows":"..\\house\\ren\\PLB404.html"}
    ]
    class HouseItem {
        plane: string[];
        solid: string[];
        dimensional: string;
        constructor(public piece: string, public etage: string, public price: string, public surface: number,public nom: string, public dimensionalConfig: {left: number; top: number; to: string}[], basePath: string, offsetPath: string) {
            this.plane = [];
            this.solid = [];
            if( !path.isAbsolute(basePath) ) {
                throw new Error(`${basePath} 不是一个绝对路径`);
            }
            basePath = path.join(basePath, nom);
            offsetPath = path.join(offsetPath, nom);
            this.addPlaneToUrl(basePath, offsetPath);
            this.addSolidToUrl(basePath, offsetPath);
            this.dimensional = path.join(offsetPath, 'PL.jpg');
        }
        addPlaneToUrl(basePath: string, offsetPath: string) {
            const dir = fs.readdirSync(path.join(basePath, '3D'));
            this.sortDir(dir);
            dir.forEach((item) => {
                this.plane.push(path.join(offsetPath,'3D' ,item));
            });
        }
        addSolidToUrl(basePath: string, offsetPath: string) {
            const dir = fs.readdirSync(path.join(basePath, 'A'));
            this.sortDir(dir);
            dir.forEach((item) => {
                this.solid.push(path.join(offsetPath,'A' ,item));
            });
        }
        sortDir(arr: string[]) {
            return arr.sort((a,b) => Number(a.match(/[0-9]+/g)?.join('')) < Number(b.match(/[0-9]+/g)?.join('')) ? -1 : 1);
        }
    }
    a.forEach(item => {
        const { etage, nom, prix: price, surface, piece } = item;
        const mouseItem = new HouseItem(piece, etage, price.toString() , surface, nom, [{ left: 100, top: 100, to: '' }], path.join(__dirname, '../public/house'), '/house/');
    })
}